package com.xquant.common.engine.xPP.response;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RectangularCoordinates {
    private List<String> xKey;
    private List<String> yKey;
    private List<String> value;

    public List<String> getxKey() {
        return xKey;
    }

    public void setxKey(List<String> xKey) {
        this.xKey = xKey;
    }

    public List<String> getyKey() {
        return yKey;
    }

    public void setyKey(List<String> yKey) {
        this.yKey = yKey;
    }

    public List<String> getValue() {
        return value;
    }

    public void setValue(List<String> value) {
        this.value = value;
    }

    public void merge(RectangularCoordinates rect){
        if(this.xKey == null || this.xKey.size() == 0
                || rect == null || rect.xKey == null || rect.xKey.size() == 0){
            return;
        }
        for(int j = 0; j< rect.xKey.size();j++){
            String xj = rect.xKey.get(j);
            String yj = rect.yKey.get(j);
            String vj = rect.value.get(j) == null?"0":rect.value.get(j);
            if(this.xKey == null) this.xKey = new ArrayList<>();
            if(this.yKey == null) this.yKey = new ArrayList<>();
            if(this.value == null) this.value = new ArrayList<>();
            Boolean flag = true;
            for(int i = 0; i< this.xKey.size();i++){
                String xi = this.xKey.get(i);
                String yi = this.yKey.get(i);
                if(xj.equals(xi) && yj.equals(xj)){
                    String vi = this.value.get(i);
                    vi = String.valueOf(vi == null ? BigDecimal.ZERO.add(new BigDecimal(vj)):new BigDecimal(vi).add(new BigDecimal(vj)));
                    this.value.set(i,vi);
                    flag =false;
                    break;
                }
            }
            if(flag){
                this.xKey.add(xj);
                this.yKey.add(yj);
                this.value.add(vj);
            }
        }

    }
}
